package com.nowcoder.community.util;

import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.* ;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.UUID;

/**
 * <p>Title: CommunityUtil</p>
 * <p>author 12427</p>
 *
 * @version ver1.0
 * @date 2022/7/6 19:00
 */
public class CommunityUtil {

    // 生成随机字符串
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-" , "") ;
    }

//    MD5加密
    public static String md5(String key){
        if(StringUtils.isBlank(key)){
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes()) ;
    }

    // map -> JSONString
    public static  String getJSONString(int code , String msg , Map<String,Object> map )  {
        JSONObject json = new JSONObject() ;
        json.put("code" , code) ;
        json.put("msg" , msg) ;
        if(map != null){
            for(String key:map.keySet()){
                json.put(key,map.get(key)) ;
            }
        }
        return json.toJSONString() ;
    }

    public static String getJSONString (int code , String msg) {
        return getJSONString(code,msg,null) ;
    }

    public static String getJSONString (int code ) {
        return getJSONString(code, null ,null) ;
    }




}
